<!--
 * @Author: 千锋爱佛僧
 * @公众号: 大前端私房菜
 * @Slogan: 千锋精品教程，好学得不像实力派！
-->
<!--
 * @Author: 千锋爱佛僧
 * @公众号: 大前端私房菜
 * @Slogan: 千锋精品教程，好学得不像实力派！
-->
<template>
  <div>
    <el-form>
      <el-form-item label="账号">
        <el-input v-model="info.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="info.password"></el-input>
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="idx" placeholder="请选择角色">
          <el-option
            v-for="(item, index) in roleList"
            :key="item.objectId"
            :label="item.name"
            :value="index"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleUser"> 新增账号 </el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { roleGet, userReg } from "@/api/user-api";
export default {
  data() {
    return {
      info: {
        username: "富贵",
        password: "123",
        roleId: "",
        roleName: "",
      },
      roleList: [],
      idx: "",
    };
  },
  watch: {
    idx(newValue) {
      console.log("idx变了", newValue); //idx的最新值
      let { objectId, name } = this.roleList[newValue];
      this.info.roleId = objectId;
      this.info.roleName = name;
    },
  },
  created() {
    roleGet().then((res) => {
      this.roleList = res.data.results;
    });
  },
  methods: {
    handleUser() {
      console.log(this.info);
      userReg(this.info); //新增账号
    },
  },
};
</script>

<style lang="scss" scoped></style>
